package com.bs.config.utils;
import java.util.*;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;


public class ListSetUtil implements ApplicationContextAware, InitializingBean {

	public ListSetUtil() {
		applicationContext = null;
		prefix = null;
	}

	public void setApplicationContext(ApplicationContext applicationContext)
			throws BeansException {
		this.applicationContext = applicationContext;
	}

	@SuppressWarnings("unchecked")
	public List getLists() {
		List list = new ArrayList();
		Map map = applicationContext.getBeansOfType(
				com.bs.config.utils.ListHelper.class, true, true);
		Set mapSet = map.entrySet();
		ListHelper helper = null;
		for (Iterator iter = mapSet.iterator(); iter.hasNext();) {
			java.util.Map.Entry element = (java.util.Map.Entry) iter.next();
			helper = (ListHelper) element.getValue();
			if (((String) element.getKey()).startsWith(prefix)
					&& helper.getMappings() != null)
				list.addAll(helper.getMappings());
		}

		return list;
	}

	public String getPrefix() {
		return prefix;
	}

	public void setPrefix(String prefix) {
		this.prefix = prefix;
	}

	public void afterPropertiesSet() throws Exception {
		if (prefix == null)
			throw new NullPointerException("prefix property can not null");
		else
			return;
	}

	private ApplicationContext applicationContext;
	private String prefix;
}

